
init();

function init() {
    //1.查询进度列表
    queryProjectList();
}


/**
 * 详情
 */
function queryProjectDetail(id,num) {
    $.ajax({
        url :   "project/queryDetail",
        type:   "post",
        data:JSON.stringify({
            projectId : id
        }),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            //console.log(data);
            var scheduleDetail = data.data;
            $("#projectName").val(scheduleDetail.projectName);
            $("#projectDesc").val(scheduleDetail.projectDesc);
            $("#projectAddr").val(scheduleDetail.projectAddr);
            $("#btn1").text("确认");
            if(num==2){
                $("#projectName").attr("disabled",false);
                $("#projectDesc").attr("disabled",false);
                $("#projectAddr").attr("disabled",false);
                $("#exampleModalLabel").text("修改项目");
                $("#btn1").text("修改");
                $("#btn1").click(function(){
                    updateProject(id);
                });
            }
        }
    });
}

/**
 * 列表
 */
function queryProjectList(pageIndex) {
    if(pageIndex == null){
        pageIndex = 0;
    }
    $.ajax({
        url :   prefixUrl + "project/queryList",
        type:   "post",
        data:JSON.stringify({
            projectName : $("#name").val(),
            pageIndex : pageIndex,
        }),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            var list = data.data.rows;
            var projectHtml  = '<thead>'
                +'<tr>'
                +'<th>序号</th>'
                +'<th>项目名称</th>'
                +'<th>操作/申请人名称</th>'
                +'<th>操作/申请时间	</th>'
                +'<th>操作</th>'
                +'</tr>'
                +'</thead>'
                +'<tbody>';
            for(var i=0; i<list.length; i++){
                projectHtml = projectHtml
                    +'<tr>'
                    +'<td>' + (parseInt(i) + 1) + '</td>'
                    +'<td>'
                    +list[i].projectName
                    +'</td>'
                    +'<td>'
                    +list[i].oprUserName
                    +'</td>'
                    +'<td>'
                    +new Date(list[i].oprDate).toLocaleDateString()
                    +'</td>'
                    +'<td>'
                    +'<button type="button" class="btn btn-info mar-r-5" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo" onclick=queryProjectDetail("'
                    + list[i].projectId
                    +'",1)><i class="glyphicon glyphicon-search"></i></button>'
                    +'<button type="button" class="btn btn-info mar-r-5" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo" onclick=queryProjectDetail("'
                    + list[i].projectId
                    +'",2)><i class="fa fa-pencil"></i></button>'
                    +'<button type="button" class="btn btn-info mar-r-5" data-toggle="modal" onclick=deleteProject("'
                    + list[i].projectId
                    +'")><i class="fa fa-bitbucket-square"></i></button>'
                    +'</td>'
                    +'</tr>';
            }
            projectHtml = projectHtml + +'</tbody>';
            $("#projectList").html(projectHtml);


            var total = data.data.total;
            var remainder = total%20;
            var allNumber = 0;
            if(remainder > 0){
                allNumber = parseInt(total/20) + 1;
            }else{
                allNumber = parseInt(total/20);
            }
            var number = 1;
            var numberHtml = '<li><a onclick=queryProjectList('+ "0" +')>«</a></li>';
            for(var i=0; i<allNumber; i++){
                var id = "query-" + i;
                if(i == 0){
                    numberHtml = numberHtml
                        +'<li id="'+ id + '" name="queryList" class="active"><a onclick=queryProjectList('+ i +')>' + number + '</a></li>';
                }else{
                    numberHtml = numberHtml
                        +'<li id="'+ id + '" name="queryList"><a onclick=queryProjectList('+ i +')>' + number + '</a></li>';
                }
                number++;
            }
            numberHtml = numberHtml + '<li><a onclick=queryProjectList('+ (allNumber-1) +')>»</a></li>';
            $("#queryList").html(numberHtml);
        }
    });
}

/**
 * 新增项目
 */
function saveProject(){
    if($("#projectName").val() == null || $("#projectName").val() == ""){
        alert("项目名称不可为空");
        return;
    }
    if($("#projectAddr").val() == null || $("#projectAddr").val() == ""){
        alert("项目地址不可为空");
        return;
    }
    if($("#projectDesc").val() == null || $("#projectDesc").val() == ""){
        alert("项目备注不可为空");
        return;
    }
    var data = {
        projectName	    :	    $("#projectName").val(),
        projectAddr	    :	    $("#projectAddr").val(),
        projectDesc     :       $("#projectDesc").val(),
    };

    $.ajax({
        url :   prefixUrl + "project/add",
        type:   "post",
        data:JSON.stringify(data),
        contentType:"application/json;",
        success:function(data) {
            alert(data.msg);
            location.href="media.html";
        }
    });

}
/**
  *删除项目
  */
function deleteProject(id) {
    if(confirm("确定要进行删除嘛?")){
        $.ajax({
            url :   prefixUrl + "project/delete",
            type:   "post",
            data:JSON.stringify({
                projectId : id
            }),
            contentType:"application/json;",
            success:function(data){
                goToLogin(data);
                if (data.code==0) {
                    alert("删除成功");
                    location.href="media.html";
                }else{
                    alert("删除失败");
                    return;
                }
            }
        });
    }
}
/**
 * 修改项目
 */
function updateProject(id){
    $.ajax({
        url :   prefixUrl + "project/update",
        type:   "post",
        data:JSON.stringify({
            projectId : id,
            projectName: $("#projectName").val(),
            projectAddr: $("#projectAddr").val(),
            projectDesc: $("#projectDesc").val()
        }),
        contentType:"application/json;",
           success:function(data){
               goToLogin(data);
            if (data.code==0) {
                alert("修改成功");
                location.href="media.html";
            }else{
                alert("修改失败");
                return;
            }

        }
    });
}